﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Globalization;
using System.Text.RegularExpressions;

namespace NuGet.Enterprise.Server.Xml.Xsl
{
    public class XsltExtensions
    {
        private static readonly Regex IdExpression = new Regex(@"Id='(?<Id>[\w._]*)'", RegexOptions.Compiled);
        private static readonly Regex VersionExpression = new Regex(@"Version='(?<Version>[\d.]*)'", RegexOptions.Compiled);

        public string FormatDate(string value, string format)
        {
            if (string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }

            DateTime date;

            if (DateTime.TryParse(value, out date))
            {
                if (string.IsNullOrEmpty(format))
                {
                    return date.ToString("s");
                }

                return date.ToString(format);
            }

            return value;
        }

        public string ParseDate(string value, string format, string culture)
        {
            if (string.IsNullOrEmpty(value))
            {
                return string.Empty;
            }

            int lcid = 0;

            if (string.IsNullOrEmpty(culture))
            {
                return ParseDate(value, format, CultureInfo.InvariantCulture);
            }
            else if (!int.TryParse(culture, out lcid))
            {
                return ParseDate(value, format, CultureInfo.GetCultureInfo(culture));
            }

            return ParseDate(value, format, CultureInfo.GetCultureInfo(lcid));
        }

        private string ParseDate(string value, string format, CultureInfo culture)
        {
            var styles = DateTimeStyles.AssumeUniversal;

            DateTime date;

            if (string.IsNullOrEmpty(format))
            {
                if (DateTime.TryParse(value, culture, styles, out date))
                {
                    return date.ToString("s");
                }
            }
            else
            {
                if (DateTime.TryParseExact(value, format, culture, styles, out date))
                {
                    return date.ToString("s");
                }
            }

            return string.Empty;
        }

        public string GetPackageId(string packageIdUrl)
        {
            if (string.IsNullOrEmpty(packageIdUrl))
            {
                return string.Empty;
            }

            var match = IdExpression.Match(packageIdUrl);

            if (!match.Success || match.Groups["Id"] == null)
            {
                return string.Empty;
            }

            return match.Groups["Id"].Value ?? string.Empty;
        }

        public string GetPackageVersion(string packageIdUrl)
        {
            if (string.IsNullOrEmpty(packageIdUrl))
            {
                return string.Empty;
            }

            var match = VersionExpression.Match(packageIdUrl);

            if (!match.Success || match.Groups["Version"] == null)
            {
                return string.Empty;
            }

            return match.Groups["Version"].Value ?? string.Empty;
        }
    }
}